package com.sise.selector;

import java.io.IOException;
import java.nio.channels.*;

public class SelectorDemo {

    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();
        //将不同的通道注册到相应到选择器上
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);

        //这里面是注册了一个 interest 集合
        SelectionKey selectionKey = socketChannel.register(selector,SelectionKey.OP_READ);

        //读取interest集合到信息 判断是否当前selectionkey包含了相应的到interest
        int interestSet =selectionKey.interestOps();
        boolean b1 = (interestSet & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT;
        boolean b2 = (interestSet & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT;
        boolean b3 = (interestSet & SelectionKey.OP_READ) == SelectionKey.OP_READ;
        boolean b4 = (interestSet & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE;

        int readySet = selectionKey.readyOps();

        selectionKey.isAcceptable();
        selectionKey.isConnectable();
        selectionKey.isReadable();
        selectionKey.isValid();
        selectionKey.isWritable();

        Channel channel = selectionKey.channel();
        Selector selector1 = selectionKey.selector();

        //可以在上边附属多余的信息
        selectionKey.attach(new Object());
        //获取附属信息
        Object param = selectionKey.attachment();

        Selector selector2 = selectionKey.selector();
        //这里会是一个堵塞的方法
        selector2.select();
        //如果不希望堵塞的话，可以立马断开
        selector2.wakeup();


    }
}
